﻿using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;
namespace SZ.Mech
{
    [StaticConstructorOnStartup]
    public class CompProperties_FlashColor : CompProperties
    {
        public bool enable;
        public CompProperties_FlashColor()
        {
            this.compClass = typeof(FlashColor);
        }
     
    }
    public class FlashColor : ThingComp
    {
        private static readonly Material LightningMat = MatLoader.LoadMat("Weather/LightningBolt");


        private CompProperties_FlashColor Props => (CompProperties_FlashColor)props;

        public override void PostSpawnSetup(bool respawningAfterLoad)
        {
            base.PostSpawnSetup(respawningAfterLoad);
            if(Props.enable)
            {
                 //LightningMat.color = new Color(0.95f, 0.35f, 0.57f, 1);

                //Material material =   LightningMat.color = new Color(0.95f, 0.35f, 0.57f, 1);
                //Material material = LightningMat;
                //Material alphaMt = FadedMaterialPool.FadedVersionOf(LightningMat, 1);

                //material.color = new Color(0.95f, 0.35f, 0.57f, 1);

                //Matrix4x4 matrix = default(Matrix4x4);
                //Vector3 drawPos = this.parent.Position.ToVector3();
                //matrix.SetTRS(drawPos, Quaternion.AngleAxis(0, Vector3.up), new Vector3(1, 1, 1));
                //Graphics.DrawMesh(LightningBoltMeshPool.RandomBoltMesh, matrix, material, 100);

                //Messages.Message($"调试，单位创建时材质{LightningMat.color}".Translate(), MessageTypeDefOf.TaskCompletion);
            }

        }

        //public override void PostDraw()
        //{
        //    base.PostDraw();
        //    if (Props.enable)
        //    {
        //        Material material = LightningMat;
        //        material.color = new Color(0.95f, 0.35f, 0.57f, 1);
        //        Messages.Message($"调试，单位创建时材质{LightningMat.color}".Translate(), MessageTypeDefOf.TaskCompletion);
        //    }

        //}


        public override void PostDestroy(DestroyMode mode, Map previousMap)
        {
            base.PostDestroy(mode, previousMap);

            if (!Props.enable)
            {
                for (float hide = 1; hide < 0; hide += 0.01f)
                {
                    Color casheColor = LightningMat.color;
                    Material alphaMt = FadedMaterialPool.FadedVersionOf(LightningMat, hide);
                    casheColor.a = alphaMt.color.a;
                    alphaMt.color = casheColor;

                    Matrix4x4 matrix = default(Matrix4x4);
                    Vector3 drawPos = this.parent.Position.ToVector3();
                    matrix.SetTRS(drawPos, Quaternion.AngleAxis(0, Vector3.up), new Vector3(1, 1, 1));
                    Graphics.DrawMesh(LightningBoltMeshPool.RandomBoltMesh, matrix, alphaMt, 100);
                    //Messages.Message($"调试，单位销毁时材质{LightningMat.color}".Translate(), MessageTypeDefOf.TaskCompletion);
                }

                //LightningMat.color = new Color(1f, 1f, 1f, 1);
                //Color casheColor = LightningMat.color;
                //Material alphaMt = FadedMaterialPool.FadedVersionOf(LightningMat, 1);
                //casheColor.a = alphaMt.color.a;
                //alphaMt.color = casheColor;



            }
            //if (!Props.enable)
            //{
            //    Material material = LightningMat;

            //material.color = new Color(1f, 1f, 1f, 1);
            //Matrix4x4 matrix = default(Matrix4x4);
            //Vector3 drawPos = this.parent.Position.ToVector3();
            //matrix.SetTRS(drawPos, Quaternion.AngleAxis(0, Vector3.up), new Vector3(1, 1, 1));
            //Graphics.DrawMesh(LightningBoltMeshPool.RandomBoltMesh, matrix, material, 100);

            //    Messages.Message($"调试，，单位死亡时材质{material.color}".Translate(), MessageTypeDefOf.TaskCompletion);
            //}

        }
    }
}

